//
//  UIViewController+extension.m
//  Civet
//
//  Created by Administrator on 15/1/20.
//  Copyright (c) 2015年 Civet. All rights reserved.
//

#import "UIViewController+extension.h"
//#import <AssetsLibrary/AssetsLibrary.h>
@implementation UIViewController (extension)


//设置title及左右Item的title
-(void)setTitle:(NSString *)title leftBarItemsTitle:(NSArray *)leftTitleArr rightBarItemsTitle:(NSArray *)rightTitleArr{
    self.navigationItem.title = title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor :TITLECOLOR,UITextAttributeFont:TITLEFONT}];
    [self.navigationController.navigationBar setBackgroundColor:TABLEVIEWCOLOR];
    self.view.backgroundColor = BACKGROUNDVIEWCOLOR;
    [self setLeftBarItemsTitle:leftTitleArr];
    [self setRightBarItemsTitle:rightTitleArr];
}

-(void)setLeftBarItemsTitle:(NSArray *)titleArr{
    [self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor :TITLECOLOR,UITextAttributeFont:TITLEFONT}];
    self.view.backgroundColor = BACKGROUNDVIEWCOLOR;
    [self.navigationController.navigationBar setBackgroundColor:TABLEVIEWCOLOR];
    NSMutableArray *leftBarItemArr = [NSMutableArray array];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -5;
    [leftBarItemArr addObject:negativeSpacer];
    if (titleArr.count>0) {
        for (NSString *title in titleArr) {
            UIButton *buttonL = [[UIButton alloc] initWithFrame:CGRectMake(0, 2.5, 80*WIDTHSCALE, 39)];
            [buttonL setBackgroundImage:[UIImage imageNamed:@"btn_bg_q.png"] forState:UIControlStateNormal];
            [buttonL setBackgroundImage:[UIImage imageNamed:@"btn_bg_h.png"] forState:UIControlStateHighlighted];
            [buttonL addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
            buttonL.titleLabel.font = TABLESECTIONFONT;
            [buttonL setTitle:title forState:UIControlStateNormal];
            UIBarButtonItem * leftDrawerButton = [[UIBarButtonItem alloc] initWithCustomView:buttonL];
            [leftDrawerButton setStyle:UIBarButtonItemStylePlain];
            [leftBarItemArr addObject:leftDrawerButton];
            UIBarButtonItem *negativeSpacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSpacer1.width = 5;
            [leftBarItemArr addObject:negativeSpacer1];
        }
        [self.navigationItem setLeftBarButtonItems:leftBarItemArr];
    }else{
        [self.navigationItem setLeftBarButtonItems:nil];
    }
}
-(void)setRightBarItemsTitle:(NSArray *)titleArr{
    [self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor :TITLECOLOR,UITextAttributeFont:TITLEFONT}];
    self.view.backgroundColor = BACKGROUNDVIEWCOLOR;
    [self.navigationController.navigationBar setBackgroundColor:TABLEVIEWCOLOR];
    NSMutableArray *rightBarItemArr = [NSMutableArray array];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -5;
    [rightBarItemArr addObject:negativeSpacer];
    if (titleArr.count>0) {
        for (NSString *title in titleArr) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 2.5, 80*WIDTHSCALE, 39)];
            [button setBackgroundImage:[UIImage imageNamed:@"btn_bg_q.png"] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"btn_bg_h.png"] forState:UIControlStateHighlighted];
            [button addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
            button.contentEdgeInsets = UIEdgeInsetsMake(5, 0, 5, 0);
            [button setTitle:title forState:UIControlStateNormal];
            button.titleLabel.font = TABLESECTIONFONT;
            
            UIBarButtonItem * rightDrawerButton = [[UIBarButtonItem alloc] initWithCustomView:button];
            [rightDrawerButton setStyle:UIBarButtonItemStylePlain];
            [rightBarItemArr addObject:rightDrawerButton];
            UIBarButtonItem *negativeSpacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSpacer1.width = 5;
            [rightBarItemArr addObject:negativeSpacer1];
        }
        [self.navigationItem setRightBarButtonItems:rightBarItemArr];
    }else{
        [self.navigationItem setRightBarButtonItems:nil];
    }
}
//设置title及左右Item的图片
-(void)setTitle:(NSString *)title leftBarItemsImg:(NSArray *)leftImgArr rightBarItemsImg:(NSArray *)rightImgArr{
    self.view.backgroundColor = BACKGROUNDVIEWCOLOR;
    [self.navigationController.navigationBar setBackgroundColor:TABLEVIEWCOLOR];
    [self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor :TITLECOLOR,UITextAttributeFont:TITLEFONT}];
        self.navigationItem.title = title;
        [self setLeftBarItemsImg:leftImgArr];
        [self setRightBarItemsImg:rightImgArr];

}

-(void)setLeftBarItemsImg:(NSArray *)imgArr{
    [self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor :TITLECOLOR,UITextAttributeFont:TITLEFONT}];
    self.view.backgroundColor = BACKGROUNDVIEWCOLOR;
    [self.navigationController.navigationBar setBackgroundColor:TABLEVIEWCOLOR];
    NSMutableArray *leftBarItemArr = [NSMutableArray array];
    if (imgArr.count>0) {
        for (NSString *imgName in imgArr) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 2.5, 39*WIDTHSCALE, 39)];
            [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
            [button setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
            [button setBackgroundImage:[UIImage imageNamed:@"btn_back_click1.png"] forState:UIControlStateHighlighted];
            button.titleLabel.font = TITLEFONT;
            UIBarButtonItem * leftDrawerButton = [[UIBarButtonItem alloc] initWithCustomView:button];
            [leftDrawerButton setStyle:UIBarButtonItemStylePlain];
            [leftBarItemArr addObject:leftDrawerButton];
        }
        [self.navigationItem setLeftBarButtonItems:leftBarItemArr];
    }else{
        [self.navigationItem setLeftBarButtonItems:nil];
    }
}
-(void)setRightBarItemsImg:(NSArray *)imgArr{
    [self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor :TITLECOLOR,UITextAttributeFont:TITLEFONT}];
    self.view.backgroundColor = BACKGROUNDVIEWCOLOR;
    [self.navigationController.navigationBar setBackgroundColor:TABLEVIEWCOLOR];
    NSMutableArray *rightBarItemArr = [NSMutableArray array];
    if (imgArr.count>0) {
        for (NSString *imgName in imgArr) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 2.5, 39*WIDTHSCALE, 39)];
            [button addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
            [button setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
            button.titleLabel.font = TITLEFONT;
            UIBarButtonItem * rightDrawerButton = [[UIBarButtonItem alloc] initWithCustomView:button];
            [rightDrawerButton setStyle:UIBarButtonItemStylePlain];
            [rightBarItemArr addObject:rightDrawerButton];
        }
        [self.navigationItem setRightBarButtonItems:rightBarItemArr];
    }else{
        [self.navigationItem setRightBarButtonItems:nil];
    }
}



















//右滑动后调至前一个页面
-(void)setupSwipeGesture{
    UISwipeGestureRecognizer *moveRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(backAction:)];
    moveRight.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:moveRight];
    
}

-(void)backAction:(id)sender
{
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)rightAction:(id)sender
{
    
}


@end
